<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Store extends Auth_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('store_mdl');
    }

    public function index()
    {
        $this->load->library('pagination');

        $path = site_url('admini/store/index');
        $url = '';
        /** 查询商家 */
        $this->db->start_cache();
        $keys = $this->input->get('keys',TRUE);
        if(!empty($keys)){
            $this->db->or_like('s_name',$keys);
            $url = '?keys='.$keys;
        }
        $this->db->stop_cache();

        $config['base_url'] = $path.$url;
        $config['total_rows'] = $this->store_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        
        $data['page'] = $this->pagination->create_links();


        //数据查询
        $per_page = $this->input->get('per_page',TRUE);
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('id desc');
        $list = $this->store_mdl->select();
        $this->db->flush_cache();
        $data['list'] = $this->store_mdl->restr($list);

        $this->_view('admin/store_index',$data);
    }

    /**
     * 添加店铺
     */
    public function create(){
        $this->_view('admin/store_create');
    }

    /**
     * 修改店铺
     */
    public function update(){
        $sid = $this->input->get('sid',TRUE);
        $sid = intval($sid);
        if(empty($sid)){
            show_404();
            exit;
        }

        $this->db->where('id',$sid);
        $res = $this->store_mdl->get_one();
        if(empty($res)){
            show_error('参数错误！');
            exit;
        }

        $data = ['res'=>$res];
        $this->_view('admin/store_update',$data);
    }

    /**
     * 店铺操作 /修改/添加
     */
    public function operation(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }
        $post = $this->input->post(NULL,TRUE);

        if(empty($post)){
            die('参数错误！');
        }

        //参数验证
        if(empty($post['s_name'])){
            die('店铺名称不能为空');
        }
        if(empty($post['contact'])){
            die('联系人不能为空');
        }
        if(empty($post['email']) || !preg_match("/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/",$post['email'])){
            die('邮箱格式不正确');
        }
        if(empty($post['phone']) || !is_numeric($post['phone'])){
            die('联系电话格式错误');
        }

        if(empty($_FILES)){
            if(empty($post['id'])){
                die('请上传logo文件');
            }
            unset($post['logo']);
        }else{
            //判断店铺是否存在
            $this->db->where('id',$post['id']);
            $res_store = $this->store_mdl->get_one();

            if(empty($res_store)){
                die('参数错误！');
            }
            //删除logo图片
            if(file_exists('.'.$res_store['logo'])){
                @unlink('.'.$res_store['logo']);
            }
            if(file_exists('./upload/store/original/'.substr($res_store['logo'],26))){
                @unlink('./upload/store/original/'.substr($res_store['logo'],26));
            }

            $this->load->library('fileupload');
            //设置表单属性
            $this->fileupload->set('path','./upload/store/original/');

            $res_upload = $this->fileupload->upload('logo');

            if($res_upload){
                $file_name = $this->fileupload->getFileName();
                //文件压缩
                $this->load->library('thumb_handler');

                $this->thumb_handler->setSrcImg('./upload/store/original/'.$file_name);
                $this->thumb_handler->setCutType(1);
                $this->thumb_handler->setDstImg('./upload/store/thumb/thumb_'.$file_name);
                //$this->thumb_handler->setMaskWord("test");

                // 指定固定宽高
                $this->thumb_handler->createImg(200,200);
                $post['logo'] = '/upload/store/thumb/thumb_'.$file_name;
            }else{
                die($this->fileupload->getErrorMsg());
            }
        }
        
        if(empty($post['id'])){
            $post['create_time'] = $_SERVER['REQUEST_TIME'];
            $this->db->set($post);
            $res = $this->store_mdl->create();
            $this->logs->add($res,'新增商铺');
        }else{
            $this->db->where('id',intval($post['id']));
            unset($post['id']);
            $this->db->set($post);
            $res = $this->store_mdl->update();
            $this->logs->add($res,'修改店铺');
        }
        
        echo $res === true ? '1' : '操作失败！请联系管理员！';
    }

    /**
     * 店铺删除
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $sid = $this->input->post('sid',TRUE);
        $sid = intval($sid);

        if(empty($sid)){
            die('参数错误！');
        }

        $res = $this->store_mdl->delete($sid);
        $this->logs->add($res,'店铺删除');
        echo $res === true ? '1' : '店铺删除失败！';
    }

}

/* End of file Store.php */
/* Location: ./application/controllers/admini/Store.php */